package leetcode.problems;

import org.junit.Test;

/**
 * Created by gmwang on 2018/7/25
 * 大写字母检测
 */
public class _0829DetectCapital {
    /**
     * Given a word, you need to judge whether Detect Capitalthe usage of capitals in it is right or not.
     * 给定一个词，你需要判断它的大写字母使用是否正确。
     * We define the usage of capitals in a word to be right when one of the following cases holds:
     * 我们用一个词来定义资本的用法，当下列情况之一成立时：
     * All letters in this word are capitals, like "USA".
     * 这个单词中的所有字母都是大写字母，比如“美国”。
     * All letters in this word are not capitals, like "leetcode".
     * 这个单词中的所有字母都不是大写字母，比如“leetcode”。
     * Only the first letter in this word is capital if it has more than one letter, like "Google".
     * 只有这个单词的第一个字母是资本，如果它有不止一个字母，比如“谷歌”。
     * Otherwise, we define that this word doesn't use capitals in a right way.
     * 否则，我们定义这个词不会用正确的方式使用大写字母。
     * Example 1:
     * Input: "USA"
     * Output: True
     * Example 2:
     * Input: "FlaG"
     * Output: False
     * /**
     *
     * @param word
     * @return
     */
    public boolean detectCapitalUse(String word) {
        char[] chars = word.toCharArray();
        if(chars.length < 2) return true;
        boolean first = (65 <= chars[0] && chars[0] <= 90)?true:false; // true 大写 // false 小写
        boolean second = (65 <= chars[1] && chars[1] <= 90)?true:false; // true 大写 // false 小写
        if(first){
        }else {
            if(second)
            return false;
        }
        for (int i = 2; i < chars.length; i++) {
            //全大写，后小写
            if(first){
                if(second && chars[i] >96){
                    return false;
                }
                //大小大
                if(!second && 65 <= chars[i] && chars[i] <= 90)
                    return false;
            }else {
                //小小大
                if(!second && 65 <= chars[i] && chars[i] <= 90)
                    return false;
            }
        }
        return true;
    }

//    public boolean detectCapitalUse(String word) {
//        /*edge case*/
//        if(word == null || word.length() == 0 ||
//                word.length() == 1)return true;
//
//        /*make sure the last n -1 words has same case*/
//        for(int i = 2; i < word.length();i++){
//            if(Character.isUpperCase(word.charAt(i)) !=
//                    Character.isUpperCase(word.charAt(i-1)))return false;
//
//        }
//        /*make sure it is not the case that the first character is*/
//        /*lowercase, the rest is uppercase*/
//        if(Character.isUpperCase(word.charAt(0)) == false &&
//                Character.isUpperCase(word.charAt(1)) == true)return false;
//        return true;
//    }
    @Test
    public void test() {
//        String a = "USA";
//        String a = "FlaG";
//        String a = "ffffffffffffffffffffF";
//        String a = "mL";
//        String a = "Ca";
//        String a = "ue";
        String a = "cVe";
        boolean res = detectCapitalUse(a);
        System.out.println(res);
    }
}
